package com.qding.park.manager.service.statis.facade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.facade.statis.service.CarStatisFacade;
import com.qding.park.manager.pojo.PowerInfo;
import com.qding.park.manager.pojo.TbCar;
import com.qding.park.manager.pojo.enums.CardStatuEnum;
import com.qding.park.manager.pojo.example.TbCarExample;
import com.qding.park.manager.pojo.example.TbCarExample.Criteria;
import com.qding.park.manager.service.statis.biz.CarStatisBiz;
import com.qding.park.manager.service.statis.mapper.TbCarStatisMapper;
import com.qding.park.manager.service.statis.mapper.TbParkspaceStatisMapper;

@Service("carStatisFacade")
public class CarStatisFacadeImpl implements CarStatisFacade {

    private static final Logger _log = LoggerFactory.getLogger(CarStatisFacadeImpl.class);
	/**
	 * @Fields tbCarMapper : TODO（车辆dao）
	 */
	@Autowired
	private TbCarStatisMapper tbCarMapper;
	
	@Autowired
    private TbParkspaceStatisMapper tbParkspaceMapper;
	
	@Autowired
	private  CarStatisBiz  carStatisBiz;

	@Override
	public ParkDataResult carlist(PowerInfo power,String parkId,String carNum, String tel,int jumpPage, int pageLength) throws BaseException {

		PageHelper.startPage(jumpPage, pageLength);

		TbCarExample example = new TbCarExample();
		example.setOrderByClause("qdpk_usercar_releation.ExpiredTime desc");
		Criteria criteria = example.createCriteria();
		criteria.andStatusNotEqualTo(CardStatuEnum.DELETED_STATUS.getStatus());
		if (!StringUtils.isBlank(carNum)) {
			criteria.andCarnumLike(carNum);
		}
		if (!StringUtils.isBlank(tel)) {
//			criteria.andTelEqualTo(tel);
			criteria.andTelLike(tel);
		}
		if (!StringUtils.isBlank(parkId)) {
			criteria.andParkIdEqualTo(Integer.valueOf(parkId));
		}
		
		criteria.andParkIdIn(power.getParkList());
		List<TbCar> carlist = tbCarMapper.listByExample(example);
		PageInfo page = new PageInfo(carlist);
		List dataList = new ArrayList();
		
		
		for(TbCar car:carlist){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("carNum", car.getCarNum());
			map.put("cardType", car.getCardType());
			map.put("tel", car.getTel());
			map.put("carNumExpiryDate",car.getCardExpiration()==null?0: car.getCardExpiration().getTime()/1000);
			map.put("carNumStatus", car.getStatus());
			dataList.add(map);
		}

		ParkDataResult parkDataResult=ParkDataResult.createListDate(dataList, page);
		return parkDataResult;
	}

	@Override
	public ParkDataResult carlist( TbCarExample example, int jumpPage, int pageLength)
			throws BaseException {
		PageHelper.startPage(jumpPage, pageLength);
		example.orderByCreateTimeDesc();
		List<TbCar> carlist = tbCarMapper.listByExample(example);
		PageInfo page = new PageInfo(carlist);
		List dataList = new ArrayList();
		
		
		for(TbCar car:carlist){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("carNum", car.getCarNum());
			map.put("carNumAttr", car.getCardName());
			map.put("cardType", car.getCardType());
			map.put("tel", car.getTel()==null?"":car.getTel());
			map.put("carNumExpiryDate",car.getCardExpiration()==null?0: car.getCardExpiration().getTime()/1000);
			map.put("carNumStatus", car.getStatus());
			map.put("carSpace", car.getCarSpace()==null?"":car.getCarSpace());
			map.put("carType", car.getCarType());
			map.put("expiryStatus", car.getExpiryStatus());
			map.put("home", car.getHomeAddr()==null?"":car.getHomeAddr());
			map.put("issueTime", car.getIssueTime()==null?0:car.getIssueTime().getTime());
			map.put("memberName", car.getMemberName()==null?"":car.getMemberName());
			map.put("monthFee", car.getMonthFee());
			map.put("operator", car.getOperator()==null?"":car.getOperator());
			map.put("parkId", car.getParkID());
			map.put("parkName", car.getParkName());
			
			dataList.add(map);
		}

		ParkDataResult parkDataResult=ParkDataResult.createListDate(dataList, page);
		return parkDataResult;
		
	}

//    @Override
//    public void userCarInfoSync() throws BaseException {
//        _log.info("线上用户、线上车辆数据同步任务开始");
//        // carBiz.userCarInfoSync();
//        // 查询所有的车场列表
//        List<TbParkspace> parkSpaceList = tbParkspaceMapper.listAll();
//
//        if (ListUtil.isEmpty(parkSpaceList)) {
//            _log.info("车场列表为空，同步任务已完成");
//            throw new ServiceCoreException(ServiceCoreException.TypeEnum.EXCEPTION_NO_DATA_FIND.getId(), null,
//                    ExceptionMessage.createExceptionMessage(
//                            ServiceCoreException.TypeEnum.EXCEPTION_NO_DATA_FIND.getDesc(), "车场列表为空", "车场列表为空"));
//        }
//        String simpleText="";
//        String detailText="";
//        _log.info("同步车场的用户数据 size=" + parkSpaceList.size());
//        for (TbParkspace park : parkSpaceList) {
//            int parkId = park.getId();
//            try {
//                carStatisBiz.userCarInfoSync(park.getId());
//            } catch (BaseException e) {
//                simpleText+=e.getExceptionMessage().getSimpleText()+"{"+parkId+"}/";
//                detailText+=e.getExceptionMessage().getDetailText()+"{"+parkId+"}/";
//            } catch (Exception e) {
//                simpleText+="未知异常{"+parkId+"}/";
//                detailText+="未知异常{"+parkId+"}/";
//            }
//        }
//        
//        if(!StringUtils.isBlank(detailText)){
//            throw new ServiceCoreException(ServiceCoreException.TypeEnum.EXCEPTION_NOT_ALL_SUCCESS.getId(), null,
//                    ExceptionMessage.createExceptionMessage(
//                            ServiceCoreException.TypeEnum.EXCEPTION_NOT_ALL_SUCCESS.getDesc(), simpleText, detailText));
//        }
//    }
//
//    @Override
//    public void userCarInfoSync(int parkId) throws BaseException {
//        _log.info("线上用户、线上车辆数据同步任务开始:" + parkId);
//        carStatisBiz.userCarInfoSync(parkId);
//    }

}
